package com.youaishujuhui.devops.web.service;

import java.util.HashMap;
import java.util.Map;

import org.nutz.http.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youaishujuhui.devops.web.util.SignTool;

@Service
public class ConfigService {

	@Autowired
	private PropertiesService propertiesService;
	
	// 获取公司、部门、项目接口
	public String loadProduct() {
		String url = "http://bigdata.gzshujuhui.com/common_config/syncData/loadProduct";
				
		String sourceProductCode = propertiesService.getConfigSourceProductCode();
		String sourceVersionCode = propertiesService.getConfigSourceVersionCode();
		String sourceApplicationCode = propertiesService.getConfigSourceApplicationCode();
		String secretKey = propertiesService.getConfigSign();
		Map<String, String> signParam = new HashMap<>();
		signParam.put("sourceProductCode", sourceProductCode);
		signParam.put("sourceVersionCode", sourceVersionCode);
		signParam.put("sourceApplicationCode", sourceApplicationCode);
				
		String sign = SignTool.sign(signParam, secretKey);
		
		Map<String, Object> postData = new HashMap<>();

		postData.put("sourceProductCode", sourceProductCode);
		postData.put("sourceVersionCode", sourceVersionCode);
		postData.put("sourceApplicationCode", sourceApplicationCode);
		postData.put("sign", sign);

		String response = Http.post(url,
				postData,
                5 * 1000);
		
		return response;
	}
	
	// 获取项目版本接口
	public String loadProductVersion(String productCode) {
		String url = "http://bigdata.gzshujuhui.com/common_config/syncData/loadProductVersion";
				
		String sourceProductCode = propertiesService.getConfigSourceProductCode();
		String sourceVersionCode = propertiesService.getConfigSourceVersionCode();
		String sourceApplicationCode = propertiesService.getConfigSourceApplicationCode();
		String secretKey = propertiesService.getConfigSign();
		Map<String, String> signParam = new HashMap<>();
		signParam.put("sourceProductCode", sourceProductCode);
		signParam.put("sourceVersionCode", sourceVersionCode);
		signParam.put("sourceApplicationCode", sourceApplicationCode);
		signParam.put("productCode", productCode);
				
		String sign = SignTool.sign(signParam, secretKey);
		
		Map<String, Object> postData = new HashMap<>();

		postData.put("sourceProductCode", sourceProductCode);
		postData.put("sourceVersionCode", sourceVersionCode);
		postData.put("sourceApplicationCode", sourceApplicationCode);
		postData.put("productCode", productCode);
		postData.put("sign", sign);

		String response = Http.post(url,
				postData,
                5 * 1000);
		
		return response;
	}
	
	// 获取项目版本接口
	public String loadApplication(String productCode, String versionCode, String applicationTypeCode) {
		String url = "http://bigdata.gzshujuhui.com/common_config/syncData/loadApplication";
				
		String sourceProductCode = propertiesService.getConfigSourceProductCode();
		String sourceVersionCode = propertiesService.getConfigSourceVersionCode();
		String sourceApplicationCode = propertiesService.getConfigSourceApplicationCode();
		String secretKey = propertiesService.getConfigSign();
		Map<String, String> signParam = new HashMap<>();
		signParam.put("sourceProductCode", sourceProductCode);
		signParam.put("sourceVersionCode", sourceVersionCode);
		signParam.put("sourceApplicationCode", sourceApplicationCode);
		signParam.put("productCode", productCode);
		signParam.put("versionCode", versionCode);
		signParam.put("applicationTypeCode", applicationTypeCode);
				
		String sign = SignTool.sign(signParam, secretKey);
		
		Map<String, Object> postData = new HashMap<>();

		postData.put("sourceProductCode", sourceProductCode);
		postData.put("sourceVersionCode", sourceVersionCode);
		postData.put("sourceApplicationCode", sourceApplicationCode);
		postData.put("productCode", productCode);
		postData.put("versionCode", versionCode);
		postData.put("applicationTypeCode", applicationTypeCode);
		postData.put("sign", sign);

		String response = Http.post(url,
				postData,
                5 * 1000);
		
		return response;
	}
	
	// 获取应用版本接口
	public String loadApplicationType() {
		String url = "http://bigdata.gzshujuhui.com/common_config/syncData/loadApplicationType";
				
		String sourceProductCode = propertiesService.getConfigSourceProductCode();
		String sourceVersionCode = propertiesService.getConfigSourceVersionCode();
		String sourceApplicationCode = propertiesService.getConfigSourceApplicationCode();
		String secretKey = propertiesService.getConfigSign();
		Map<String, String> signParam = new HashMap<>();
		signParam.put("sourceProductCode", sourceProductCode);
		signParam.put("sourceVersionCode", sourceVersionCode);
		signParam.put("sourceApplicationCode", sourceApplicationCode);
				
		String sign = SignTool.sign(signParam, secretKey);
		
		Map<String, Object> postData = new HashMap<>();

		postData.put("sourceProductCode", sourceProductCode);
		postData.put("sourceVersionCode", sourceVersionCode);
		postData.put("sourceApplicationCode", sourceApplicationCode);
		postData.put("sign", sign);

		String response = Http.post(url,
				postData,
                5 * 1000);
		
		return response;
	}
}
